package service.impl;

import dao.AdminDao;
import dao.impl.AdminDaoimpl;
import pojo.Admin;
import pojo.PageBean;
import pojo.User;
import service.AdminService;

import java.sql.SQLException;
import java.util.List;

/**
 * @author LiangHaiBo
 * @version 1.0
 * @date 2021.8.31 上午 8:38
 */
public class AdminServiceimpl  implements AdminService {
    @Override
    public boolean isLogin(String name, String password) {
        AdminDao adminDao = new AdminDaoimpl();
        Admin admin = adminDao.findAllByname(name);
        if(admin != null ){
            if(admin.getAdminpassword().equals(password)){
                return true;
            }
        }
        return false;
    }

    @Override
    public List<User> getAllUser() {
        AdminDao adminDao = new AdminDaoimpl();
        List<User> users = adminDao.selectUserAll();
        if(users != null){
            return users;
        }
        return null;
    }

    @Override
    public User getUser(String uid) {
        AdminDao adminDao = new AdminDaoimpl();
        User user = adminDao.getUserById(uid);
        if(user != null){
            return user;
        }
        return null;
    }

    @Override
    public void updateget(User user) {
        AdminDao adminDao = new AdminDaoimpl();
        adminDao.update(user);
    }

    @Override
    public void deleteGet(String uid) {
        AdminDao adminDao = new AdminDaoimpl();
        adminDao.delete(uid);
    }

    @Override
    public PageBean<User> getUserByPage(int currentPage, int pageSize) {
        //当前页码,每页条数,分页数据,总计录数
        try {
            AdminDao adminDao = new AdminDaoimpl();
            List<User> list = adminDao.findUserByPage(currentPage, pageSize);

            int totalCount  = adminDao.selectTotalCount();

            return new PageBean<User>(currentPage,pageSize,totalCount,list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
